app.controller('recordCtrl', function($rootScope, $scope, httpService, $timeout, $localStorage, wtmapService){
    $scope.searchParams = {};
    $scope.searchParams.label="提交时间";
    $scope.count = 0;
    $scope.RecordType = wtSys.Emun.RecordType; //记录类型
    $scope.searchList = [{
        "typeName" : "类型",
        "typeList" : [{"Id" : 3, "Name" : "巡查反馈"},
            {"Id" : 4, "Name" : "维修反馈"}
        ],
        "isRadio" : true,
    }];

    /*==============设置选择设施位置信息 Begin=============================*/
    $scope.mapheight = ($(".rgt-wrap").height() - 85) || 700;
    $scope.mapIntialed = function(scope){
        var promise = wtmapService.intialMap('map1', false, 0, $rootScope.arcgisConfig.lods); //同步调用，获取承诺接口
        promise.then(function(mapobj){
            //添加底图（本系统是cad发布的图层）
            mapobj.addDynamicMap($rootScope.arcgisConfig.baseMap);
            //默认中心点
            mapobj.zoomTobyXY(175307.451, 135762.144, 15);
            //将mapobj对像暴露到作用域中
            $scope.mapobj = mapobj;
        }, function(err){
            console.log(err);
        });
    };
    /**
     * 绘制记录点信息
     * @param mapobj 地图对像
     * @param recordPoints [points with isFinished]
     */
    $scope.drawRecordPoint = function(mapobj, recordPoints){

        if(!mapobj){
            $timeout(function(){
                $scope.drawRecordPoint($scope.mapobj, recordPoints);
            }, 500);
            return;
        }
        var DeviceDrawOption = {
            "layerid" : "device",
            //title : item.Name,
            "symbolUrl" : "/assets/img/Icons/patron_巡查点32.png",
            "symbolStyle" : {
                "height" : 32,
                "width" : 32,
                "angle" : 0,
                xOffset : 0,
                yOffset : 16
            }
        };
        var FlagDrawOption = {
            "layerid" : "flag",
            //title : item.Name,
            "symbolUrl" : "/assets/img/Icons/聚焦框64.png",
            symbolStyle : {
                width : 34,
                height : 34,
                angle : 0,
                xOffset : 0,
                yOffset : 17

            }
        };
        var points = [];
        //0、清除原有的图层
        mapobj.drawApi.clearGraphics("device");
        mapobj.drawApi.clearGraphics("flag");

        //1、绘制任务点及完成
        recordPoints.forEach(function(item, index){
            points.push([item.Lng, item.Lat]);
            mapobj.drawApi.drawPoint(item.Lng, item.Lat, DeviceDrawOption);
            if(item.IsFinish === true){
                mapobj.drawApi.drawPoint(item.Lng, item.Lat, FlagDrawOption);
            }
        });
        //2、获取定位中点
        var centerPoint = mapobj.getCenterPoint(points);
        mapobj.zoomTobyXY(centerPoint.x, centerPoint.y, 15);

    };
    /*==============设置选择设施位置信息 End=============================*/

    // 初始化查询变量
    $scope.initParam = function(){
        $scope.recordList = [];
        $scope.page = 1;
        $scope.busy = false;
        $scope.limit = 10;
        $scope.isGetDetailTag = true;//第一次默认加载第一条记录右侧信息
    }
    //获取工作记录列表
    $scope.getRecordList = function(isSearch){
        if(isSearch){
            $scope.initParam();
        }
        if($scope.busy) return false;
        var key = $scope.key ? $scope.key : "";
        var recordtype = "";
        if($scope.searchModel){
            var arr = $scope.searchModel;
            if(arr.length > 0){
                var recordtypeArr = [];
                arr.forEach(function(item, index){
                    recordtypeArr.push(item.Id);
                });
                recordtype = recordtypeArr.join(",");
            }
        }
        var begintime = $scope.searchParams.begintime;
        begintime = begintime ? moment(begintime).format("YYYY-MM-DD") : "";
        var endtime = $scope.searchParams.endtime;
        endtime = endtime ? moment(endtime).format("YYYY-MM-DD") : "";
        var path = '/record?page=' + $scope.page + '&limit=' + $scope.limit + '&filter=key:' + key + ";recordtype:" + recordtype + ";begintime:" + begintime + ";endtime:" + endtime;
        $scope.busy = true;
        httpService.request("GET", path, null, function(result){
            $scope.count = result.count;
            angular.forEach(result.data, function(data, i){
                $scope.recordList.push(data);
            });
            if(result.data.length >= $scope.limit){
                $scope.busy = false;
                $scope.page += 1;
            }
            if($scope.recordList.length > 0 && $scope.isGetDetailTag){
                $scope.isGetDetailTag = false;
                var arr = $scope.recordList;
                $scope.activeId = arr[0].Id;//默认查询第一条记录的详情
            }
        });
    }
    //获取工作记录详情
    $scope.getRecordDetail = function(){
        var path = '/record/' + $scope.activeId;
        httpService.request("GET", path, null, function(result){
            $scope.recordDetailModel = result.data;
            // 被选中点的位置(设置默认值)
            // console.log(result.data.Location);
            $scope.drawRecordPoint($scope.mapobj, result.data.Location);


        });
    }
    //获取右侧model
    $scope.getRgtModel = function(){
        $scope.getRecordDetail();
    }
    $scope.initParam();// 初始化查询列表变量
    $scope.getRecordList();// 获取工作记录列表
});
